﻿using PagedList;
using SIG.Infrastructure.Configs;
using SIG.Model;
using SIG.Service;
using SIG.ViewModel.Admin;
using System;
using System.Web.Mvc;

namespace SIG.Web.Areas.Admin.Controllers
{
    public class LogsController : Controller
    {
        ILogService _LogService;

        public LogsController(ILogService LogService)
        {
            _LogService = LogService;

        }

        // GET: Logs
        //[Authorize(Roles = "创始人,管理员")]
        public ActionResult Index(int? page, DateTime? startDate, DateTime? expireDate, string level)
        {
            var logSearchVM = new LogSearchVM();

            logSearchVM.StartDate = startDate;
            logSearchVM.ExpireDate = expireDate;
            logSearchVM.Level = level;

            logSearchVM.PageIndex = (page ?? 1) - 1;
            logSearchVM.PageSize = SettingsManager.Log.PageSize;
            logSearchVM.Logs = _LogService.SearchLogs(logSearchVM.PageIndex, logSearchVM.PageSize, startDate, expireDate, level);
            logSearchVM.TotalUserCount = _LogService.SearchLogsCount(startDate, expireDate, level);

            var logsAsIPagedList = new StaticPagedList<Log>(logSearchVM.Logs, logSearchVM.PageIndex + 1, logSearchVM.PageSize, logSearchVM.TotalUserCount);
            ViewBag.OnePageOfLogs = logsAsIPagedList;

            return View(logSearchVM);

        }


        // DELETE: Admin/Logs/Delete
        [HttpPost]
        //[Authorize(Roles = "创始人,管理员")]
        public JsonResult Delete(string id)
        {
            if (id == "all")
            {
                int count = _LogService.RemoveAll();                
                if (count>0)           
                  return Json("all", JsonRequestBehavior.DenyGet);
            }
            else
            {
                
                    int logId;
                    Int32.TryParse(id, out logId);

                    var log = _LogService.GetById(logId);
                    _LogService.Delete(log);

                    return Json("one", JsonRequestBehavior.DenyGet);
               
            }

           
            return Json(false, JsonRequestBehavior.DenyGet);
        }
            
    }
}